package com.bwie.controller;

import com.bwie.domain.TbUser;
import com.bwie.mapper.TbUserMapper;
import com.bwie.pay.AlipayUtils;
import com.bwie.pay.WxPayUtils;
import com.bwie.poi.ExcelUtil;
import com.bwie.utils.KaptchaUtils;
import com.bwie.utils.QrCodeUtils;
import com.bwie.utils.R;
import com.bwie.vo.CheckUserVo;
import com.bwie.vo.PageInfoVo;
import com.bwie.vo.TestLoginVo;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2024/10/22 10:28
 */

@RestController
@RequestMapping(value = "/user/test")
@Slf4j
public class TestController {

    @Autowired
    KafkaTemplate kafkaTemplate;

    @Autowired
    TbUserMapper tbUserMapper;

    /**
     * 发送kafka测试消息
     * @param msg
     * @return
     */
    @PostMapping(value = "/sendMessage/{msg}")
    public R sendMessage(@PathVariable("msg") String msg) {

        kafkaTemplate.send("topic-test", msg);

        return R.OK();
    }

    @KafkaListener(topics = "topic-test")
    public void recvMessage(String message, Acknowledgment acknowledgment) {
        System.out.println("接收到消息：" + message);
        // 手动确认消息
        acknowledgment.acknowledge();
    }

    @GetMapping(value = "/exportUsers")
    public void exportUsers(HttpServletResponse response) {
        List<TbUser> tbUsers = tbUserMapper.selectList(null);
        ExcelUtil<TbUser> excelUtil = new ExcelUtil<TbUser>(TbUser.class);
        excelUtil.exportExcel(response, tbUsers, "用户数据");
    }

    @GetMapping("/qrcode")
    public void generateQRCode(HttpServletResponse response) throws WriterException, IOException {
        String qrCodeText = "https://www.example.com"; // 你要编码的文本
        int size = 300; // 二维码图像的大小

        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hints);

        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);

        // 将BufferedImage转换为byte数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", baos);
        byte[] imageBytes = baos.toByteArray();

        // 设置响应头
        response.setContentType("image/png");
        response.setContentLength(imageBytes.length);

        // 将图像数据写入响应输出流
        response.getOutputStream().write(imageBytes);
        response.getOutputStream().flush();
    }

    @GetMapping("/testQRCode")
    public void testQRCode(HttpServletResponse response) {
        QrCodeUtils.createQrCode(response, "http://www.baidu.com");
    }

    @GetMapping("/aliPay")
    public void aliPay(HttpServletResponse response) {
        String orderNo = AlipayUtils.getOrderNo();
        System.out.println("阿里订单号："+orderNo);
        if(orderNo != null) {
            String codeUrl = AlipayUtils.postOrderObj(orderNo, "测试订单", 100);
            System.out.println("阿里支付："+codeUrl);
            QrCodeUtils.createQrCode(response, codeUrl);
        }
    }

    @GetMapping("/weChatPay")
    public void weChatPay(HttpServletResponse response) {
        String orderNo = WxPayUtils.getOrderNo();
        System.out.println("微信订单号："+orderNo);
        if(orderNo != null) {
            String codeUrl = WxPayUtils.postOrderObj(orderNo, "测试订单", 1);
            System.out.println("微信支付："+codeUrl);
            QrCodeUtils.createQrCode(response, codeUrl);
        }
    }
    @GetMapping("/checkWxPay")
    public void checkWxPay(String orderNo) {
        System.out.println("订单号："+orderNo);
        boolean b = WxPayUtils.checkPayStatus(orderNo);
        System.out.println("支付状态："+b);
    }

    @GetMapping("/checkAliPay")
    public void checkAliPay(String orderNo) {
        System.out.println("订单号："+orderNo);
        boolean b = AlipayUtils.checkPayStatus(orderNo);
        System.out.println("支付状态："+b);
    }

    @GetMapping(value = "/captcha")
    public void makeCaptcha(HttpServletResponse response, String captchaCode) {
        KaptchaUtils.generateCaptcha(response, captchaCode);
    }

    @PostMapping(value = "/validator")
    public R getValidator(@Valid @RequestBody CheckUserVo checkUserVo) {
        System.out.println(checkUserVo);
        return R.OK();
    }

    @PostMapping(value = "/testPage")
    public  R testPage(@Validated @RequestBody PageInfoVo pageInfoVo) {
        System.out.println(pageInfoVo);
        return R.OK();
    }

    @PostMapping(value = "/testParameter")
    public R testParameter(@Validated @RequestBody TestLoginVo testLoginVo) {
        System.out.println(testLoginVo);
        return R.OK();
    }
}
